package com.zrf.gulimall.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.common.database.utils.Query;
import com.zrf.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.zrf.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zrf.gulimall.product.service.AttrAttrGroupRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrGroupRelationService {

	@Resource
	private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<AttrAttrgroupRelationEntity> page = this.page(
				new Query<AttrAttrgroupRelationEntity>().getPage(params),
				new QueryWrapper<AttrAttrgroupRelationEntity>()
		);

		return new PageUtils(page);
	}

	@Override
	public List<AttrAttrgroupRelationEntity> queryByAttrIds(List<Long> attrIdList) {
		return this.lambdaQuery().in(CollUtil.isNotEmpty(attrIdList), AttrAttrgroupRelationEntity::getAttrId, attrIdList)
				.list();
	}

	@Override
	public AttrAttrgroupRelationEntity queryByAttrId(Long attrId) {
		return this.lambdaQuery().eq(Objects.nonNull(attrId), AttrAttrgroupRelationEntity::getAttrId, attrId)
				.one();
	}

	@Override
	public List<AttrAttrgroupRelationEntity> queryByAttrGroupId(Long attrGroupId) {
		return this.lambdaQuery().eq(Objects.nonNull(attrGroupId), AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId)
				.list();
	}

	@Override
	public List<AttrAttrgroupRelationEntity> queryByAttrGroupIds(List<Long> attrGroupIdList) {
		return this.lambdaQuery().in(CollUtil.isNotEmpty(attrGroupIdList), AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupIdList)
				.list();
	}

	@Override
	public void updateByAttrId(Long attrId, Long attrGroupId) {
		this.lambdaUpdate()
				.set(Objects.nonNull(attrGroupId), AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId)
				.eq(AttrAttrgroupRelationEntity::getAttrId, attrId)
				.update();
	}

	@Override
	public void delete(List<AttrAttrgroupRelationEntity> relationEntities) {
		attrAttrgroupRelationDao.deleteBatch(relationEntities);
	}

}